<?php

namespace app\service;


use app\model\CarModel;
use app\model\Driver;
use app\model\DriverModel;
use app\model\LineModel;
use app\model\Networks;
use app\model\NetworksModel;
use app\model\ParnterLine;
use app\model\PartnerLogModel;
use app\model\PartnerUserModel;
use app\model\PartnerUser;
use app\model\WarehouseModel;
use think\facade\Db;

class LineService
{


    public static function lists($param)
    {
        return LineModel::lists($param);
    }

    public static function add($data, $logParam)
    {
        Db::startTrans();
        $data['line_no'] = "L" . GetRandInt(8);
        $data['add_time'] = getNowDateTime();
        $length = 0;
        $data = self::getData($data, $length);

        $res = ParnterLine::create($data);



        //把路线id 写入网点表
        $up = Networks::whereIn('id',$data['network_ids'])->update(['parnter_line_id'=>$res->id,'is_parnter_line'=>1]);
        //更新司机表车的信息,否则新车无法正常领任务
        $driver = Driver::where('id',$data['defaut_driver_id'])->update(['today_car_id'=>$data['default_car_id']]);


        if ($up && $res && $driver) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function edit($data, $logParam, $id)
    {
        Db::startTrans();
        $data['update_time'] = getNowDateTime();
        $length = 0;
        $parnterLine = ParnterLine::where('id',$id)->find();
        if(!$parnterLine) return 10003;
        Networks::whereIn('id',$parnterLine->network_ids)->update(['parnter_line_id'=>'','is_parnter_line'=>2]);


        $data = self::getData($data, $length);

        $res = ParnterLine::where('id',$id)->update($data);
        //把路线id 写入网点表1
        $up = Networks::whereIn('id',$data['network_ids'])->update(['parnter_line_id'=>$id,'is_parnter_line'=>1]);

        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '修改线路信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res  && $up) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function del($data, $logParam, $id)
    {
        Db::startTrans();
        $res = LineModel::del($id);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '删除线路信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    /**
     * @param $data
     * @param float $length
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getData($data, float $length)
    {
        $networkResult = Networks::whereIn('id', $data['network_ids'])->select();

        $networkResultCount = count($networkResult);
        $max_time           = 0;
        foreach ($networkResult as $k => $v) {
            if ($k < ($networkResultCount - 1)) {
                $length += GetDistance($networkResult[$k]['longitude'], $networkResult[$k]['dimension'], $networkResult[$k + 1]['longitude'], $networkResult[$k]['dimension']);
            }
            if ($v['end_time'] > $max_time) {
                $max_time = $v['end_time'];
            }
        }
        $data['total_mileage']        = ($length / 1000);
        $data['merchant_id']          = 1;
        $data['time_consume']         = ($length / 1000) / 60;
        $data['latest_delivery_time'] = $max_time;
        return $data;
    }
}